<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题编号" prop="problemId">
        <el-input
          v-model="queryParams.problemId"
          placeholder="请输入问题编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.feedback_order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="货值" prop="goodsValue">
        <el-input
          v-model="queryParams.goodsValue"
          placeholder="请输入货值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="PP打款金额" prop="ppPaymentAmount">
        <el-input
          v-model="queryParams.ppPaymentAmount"
          placeholder="请输入PP打款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实时汇率" prop="realTimeExchangeRate">
        <el-input
          v-model="queryParams.realTimeExchangeRate"
          placeholder="请输入实时汇率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="应付款" prop="amountPayable">
        <el-input
          v-model="queryParams.amountPayable"
          placeholder="请输入应付款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返款日期">
        <el-date-picker
          v-model="daterangeRefundDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="运营负责人" label-width="80" prop="operationManager">
        <el-input
          v-model="queryParams.operationManager"
          placeholder="请输入运营负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测评负责" prop="evaluationManager">
        <el-input
          v-model="queryParams.evaluationManager"
          placeholder="请输入测评负责"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="PP汇率" prop="ppExchangeRate">
        <el-input
          v-model="queryParams.ppExchangeRate"
          placeholder="请输入PP汇率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PP手续费" prop="ppHandlingFee">
        <el-input
          v-model="queryParams.ppHandlingFee"
          placeholder="请输入PP手续费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:feedback:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:feedback:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:feedback:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="问题编号" align="center" prop="problemId" />
      <el-table-column label="单号" align="center" prop="orderNo" />
      <el-table-column label="订单类型" align="center" prop="orderType" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.feedback_order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单图" align="center" prop="orderImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.orderImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="反馈图" align="center" prop="feedbackImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.feedbackImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="货值" align="center" prop="goodsValue" />
      <el-table-column label="PP汇率(%)" align="center" prop="ppExchangeRate" />
      <el-table-column label="PP手续费" align="center" prop="ppHandlingFee" />
      <el-table-column label="PP打款金额" align="center" prop="ppPaymentAmount" />
      <el-table-column label="实时汇率" align="center" prop="realTimeExchangeRate" />
      <el-table-column label="佣金" align="center" prop="brokerage" />
      <el-table-column label="应付款" align="center" prop="amountPayable" />
      <el-table-column label="返款日期" align="center" prop="refundDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运营负责人" align="center" prop="operationManager" />
      <el-table-column label="测评负责" align="center" prop="evaluationManager" />
      <!-- <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:feedback:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改反馈信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题编号" label-width="100px" prop="problemId">
          <!-- <el-input v-model="form.problemId" placeholder="请输入问题编号" /> -->
           <el-select v-model="form.problemId" @change="handleSelectProblemId" filterable placeholder="请选择角色">
                <el-option
                  v-for="item in problemOptions"
                  :key="item.id"
                  :label="' 问题编号：' + item.id + ' 搜索词：' + item.searchWord + ' 店铺名：' + item.shopName"
                  :value="item.id"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="单号" label-width="100px" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入单号" />
        </el-form-item>
        <el-form-item label="订单类型" label-width="100px" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option
              v-for="dict in dict.type.feedback_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单图" label-width="100px" prop="orderImage">
          <image-upload v-model="form.orderImage"/>
        </el-form-item>
        <el-form-item label="反馈图" label-width="100px" prop="feedbackImage">
          <image-upload v-model="form.feedbackImage"/>
        </el-form-item>
        <el-form-item label="货值" label-width="100px" prop="goodsValue">
          <el-input-number v-model="form.goodsValue" :min="0.00" :precision="2" :step="0.01" @input="handleGoodsValueInput" placeholder="请输入货值" />
        </el-form-item>
        <el-form-item label="PP汇率(%)" label-width="100px" prop="ppExchangeRate">
          <el-input-number v-model="form.ppExchangeRate" :precision="2" :step="0.01" disabled placeholder="请输入PP汇率" />
        </el-form-item>
        <el-form-item label="PP手续费" label-width="100px" prop="ppHandlingFee">
          <el-input-number v-model="form.ppHandlingFee" :precision="2" :step="0.01" disabled placeholder="请输入PP手续费" />
        </el-form-item>
        <el-form-item label="PP打款金额" label-width="100px" prop="ppPaymentAmount">
          <el-input-number v-model="form.ppPaymentAmount" :precision="2" :step="0.01" disabled placeholder="请输入PP打款金额" />
        </el-form-item>
        <el-form-item label="实时汇率" label-width="100px" prop="realTimeExchangeRate">
          <el-input-number v-model="form.realTimeExchangeRate" :min="0.00" :precision="2" :step="0.01" @input="handleRealTimeExchangeRateInput" placeholder="请输入实时汇率" />
        </el-form-item>
        <el-form-item label="佣金" label-width="100px" prop="brokerage">
          <el-input-number v-model="form.brokerage" :min="0.00" :precision="2" :step="0.01" @input="handleRealTimeExchangeRateInput" placeholder="请输入实时汇率" />
        </el-form-item>
        <el-form-item label="应付款" label-width="100px" prop="amountPayable">
          <el-input-number v-model="form.amountPayable"  :precision="4" :step="0.01" disabled placeholder="请输入应付款" />
        </el-form-item>
        <el-form-item label="返款日期" label-width="100px" prop="refundDate">
          <el-date-picker clearable
            v-model="form.refundDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择返款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运营负责人" label-width="100px" prop="operationManager">
          <el-input v-model="form.operationManager" disabled placeholder="请输入运营负责人" />
        </el-form-item>
        <el-form-item label="测评负责" label-width="100px" prop="evaluationManager">
          <el-input v-model="form.evaluationManager" placeholder="请输入测评负责" />
        </el-form-item>
        <el-form-item label="备注" label-width="100px" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/system/feedback";
import { listProblem, noPagelistProblem, getProblem, delProblem, addProblem, updateProblem } from "@/api/system/problem";

export default {
  name: "Feedback",
  dicts: ['feedback_order_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 反馈信息表格数据
      feedbackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeRefundDate: [],
      // 备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        problemId: null,
        orderNo: null,
        orderType: null,
        orderImage: null,
        feedbackImage: null,
        goodsValue: null,
        ppPaymentAmount: null,
        realTimeExchangeRate: null,
        amountPayable: null,
        refundDate: null,
        operationManager: null,
        evaluationManager: null,
        ppExchangeRate: null,
        ppHandlingFee: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        problemId: [
          { required: true, message: "问题编号不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "单号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "blur" }
        ],
        orderImage: [
          { required: true, message: "订单图不能为空", trigger: "blur" }
        ],
        feedbackImage: [
          { required: true, message: "反馈图不能为空", trigger: "blur" }
        ],
        goodsValue: [
          { required: true, message: "货值不能为空", trigger: "blur" }
        ],
        ppExchangeRate: [
          { required: true, message: "PP汇率不能为空", trigger: "blur" }
        ],
        ppHandlingFee: [
          { required: true, message: "PP手续费不能为空", trigger: "blur" }
        ],
        ppPaymentAmount: [
          { required: true, message: "PP打款金额不能为空", trigger: "blur" }
        ],
        realTimeExchangeRate: [
          { required: true, message: "实时汇率不能为空", trigger: "blur" }
        ],
        amountPayable: [
          { required: true, message: "应付款不能为空", trigger: "blur" }
        ],
        refundDate: [
          { required: true, message: "返款日期不能为空", trigger: "blur" }
        ],
        operationManager: [
          { required: true, message: "运营负责人不能为空", trigger: "blur" }
        ]
      },
      problemOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询反馈信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRefundDate && '' != this.daterangeRefundDate) {
        this.queryParams.params["beginRefundDate"] = this.daterangeRefundDate[0];
        this.queryParams.params["endRefundDate"] = this.daterangeRefundDate[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        problemId: null,
        orderNo: null,
        orderType: null,
        orderImage: null,
        feedbackImage: null,
        goodsValue: null,
        ppPaymentAmount: null,
        realTimeExchangeRate: null,
        brokerage: null,
        amountPayable: null,
        refundDate: null,
        operationManager: null,
        evaluationManager: null,
        ppExchangeRate: null,
        ppHandlingFee: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRefundDate = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.ppExchangeRate = 4.4;
      this.form.ppHandlingFee = 0.49;
      this.form.brokerage = 0.00;
      this.getProblemOptions();
      this.open = true;
      this.title = "添加反馈信息";
    },
    // 选中问题下拉列表时触发
    handleSelectProblemId(value){
      getProblem(value).then(response => {
        this.form.operationManager = response.data.operationManager;
      });
    },
    getProblemOptions() {
      noPagelistProblem().then(response => {
        this.problemOptions = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeedback(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改反馈信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除反馈信息编号为"' + ids + '"的数据项？').then(function() {
        return delFeedback(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/feedback/export', {
        ...this.queryParams
      }, `反馈信息_${new Date().getTime()}.xlsx`)
    },
    // 计算pp打款金额
    handleGoodsValueInput(){
      if(this.form.goodsValue != null && this.form.goodsValue > 0){
        const totalMoney = this.form.goodsValue + this.form.ppHandlingFee;
        const exchangeRate = 1 - this.form.ppExchangeRate / 100;
        this.form.ppPaymentAmount = totalMoney / exchangeRate;
      }
    },
    // 计算应付款金额
    handleRealTimeExchangeRateInput(){
      if(this.form.ppPaymentAmount != null && this.form.ppPaymentAmount >= 0 && this.form.realTimeExchangeRate != null && this.form.realTimeExchangeRate >= 0){
        const amountPayable = this.form.ppPaymentAmount * this.form.realTimeExchangeRate + this.form.brokerage;
        this.form.amountPayable = amountPayable.toFixed(4);
      }
    }
  }
};
</script>
